##############################################################################
# File-Name: 06_analysis_descriptive_whatsapp_usage.r
# Purpose: Analysis primary outcomes
# Machine: macOS Ventura 13.5.1
# R version 4.3.1 (2023-06-16)
##############################################################################

# Packages ----------------------------------------------------------------
pacman::p_load(tidyverse, scales,  knitr, srvyr, extrafont, 
               rebus, broom, tidyr, lubridate, here, ggdist, ggtext, ggalt, janitor, 
               broom, tidyr,  stargazer, janitor, estimatr, list)

# Source Auxiliary Code ---------------------------------------------------
source(here("code","_utils.r"))



# Data wrangling ----------------------------------------------------------

# open
d <- readRDS(here("data","all_processed_data.rds"))


# Figure 18 - WhatsApp usage ------------------------------------------------------------

# build usage based on the outcomes whatsapp_purposes
usage = d %>%
    select(response_id, contains("whatsapp_purposes")) %>%
    pivot_longer(cols=-response_id, 
                 names_to = "types",
                 values_to = "f") %>%
    mutate(frequency=case_when(str_detect(f, "a day") ~ "At least once a day",  
                               TRUE ~ as.character(f))) %>%
    count(types, frequency) %>%
    filter(!is.na(frequency)) %>%
    mutate(label=case_when(str_detect(types, "family") ~ "To communicate with family and friends", 
                           str_detect(types, "groups") ~ "To participate in groups",
                           str_detect(types, "news") ~"To stay up-to-date with political \n developments in my country",
                           str_detect(types, "bills") ~ "To pay bills, do business, and work",
                           str_detect(types, "politics") ~ "To send and receive information \n about politics to my friends"), 
           types=fct_relevel(types, "w1_q_whatsapp_purposes_family", "w1_q_whatsapp_purposes_news", "w1_q_whatsapp_purposes_politics", 
                             "w1_q_whatsapp_purposes_groups", "w1_q_whatsapp_purposes_pay_bills")) %>%
    arrange(types) %>%
    mutate(label=fct_inorder(label), 
           frequency=fct_relevel(frequency, "At least once a day")) %>%
    filter(frequency!="Don’t Know") %>%
    group_by(label) %>%
    mutate(total=sum(n), 
           prop=n/total, 
           frequency=str_replace_all(frequency, "days", "days \n "), 
           frequency=str_replace_all(frequency, "once", "once \n "), 
           frequency=fct_relevel(frequency, c("At least once \n  a day", "1 to 2 days \n  a week", 
                                              "3 to 6 days \n  a week", "Every few weeks"))) 


# graph
treat=wesanderson::wes_palette("BottleRocket2")[[3]]
control = "gray90"
ggplot(usage, 
       aes(y=prop,
           x=frequency)) +
    geom_bar(width = .8, color="black",
             position = position_dodge(width = 0.6),
             alpha=.8, 
             color="black",
             stat="identity", 
             fill=treat) +
    facet_wrap(~label) +
    xlab("\n Self-Reported WhatsApp Usage")+
    ylab("Share of Valid Responses") +
    labs(title="Use WhatsApp to:") +
    theme(axis.title.x = element_text(hjust=0.5, face="italic"),
          axis.text = element_text(size=12),
          strip.text = element_text(family = my_font, 
                                    color = "#22211d",
                                    size = 14, 
                                    face="bold"), 
          panel.background = element_rect(fill = "gray92"),
          panel.grid =  element_blank(), 
          strip.background = element_rect(fill="gray92", color="gray92"))  


#overleaf output

ggsave(filename = here("output", "sm_fig18.png"), 
       width = 18, height = 14, units = "in", 
       pointsize = 12, bg = "white", 
       dpi=300)


# Figure 16 - Overall WhatsApp usage --------------------------------------------------

# use self-reported usage to measure it in the pre-treatment
usage = d %>%
    select(response_id, w1_q_whatsapp, q_whatsapp) %>%
    pivot_longer(cols = -response_id, 
                 values_to="f", 
                 names_to="wave") %>%
    mutate(wave_=ifelse(str_detect(wave, "w1"), "Pre-Treatment", "Post-Treatment")) %>%
    count(wave_,f) %>%
    group_by(wave_) %>%
    mutate(total=sum(n), 
           prop=n/total, 
           wave_=as.factor(wave_), 
           wave_=fct_rev(wave_))


ggplot(usage %>% 
           filter(wave_=="Pre-Treatment"), 
       aes(y=n,
           x=f)) +
    geom_bar(width = .8, color="black",
             position = position_dodge(width = 0.6),
             alpha=.8, 
             color="black",
             stat="identity", 
             fill=treat) +
    facet_wrap(~wave_) +
    xlab("\n Share of Valid Responses")+
    ylab("Number of Respondents") +
    labs(title="", 
         caption = "Participants who reported using WhatsApp less than 10 minutes per day were screened out of the experiments") +
    theme(axis.title.x = element_text(hjust=0.5, face="italic"),
          axis.text = element_text(size=12, angle = 45, hjust=1),
          strip.text = element_text(family = my_font, 
                                    color = "#22211d",
                                    size = 14, 
                                    face="italic"), 
          panel.background = element_rect(fill = "gray92"),
          panel.grid =  element_blank(), 
          strip.background = element_rect(fill="gray92", color="gray92"))  


save_function("sm_fig16.png")

# Fig 17 - Receive image about politics --------------------------------------------

# use self-reported exposure to multimedia about politics
usage = d %>%
    select(response_id, w1_q_whatsapp_image) %>%
    pivot_longer(cols = -response_id, 
                 values_to="f", 
                 names_to="wave") %>%
    mutate(f=case_when(str_detect(f, "a day") ~ "At least once a day",  
                       TRUE ~ as.character(f))) %>%
    count(wave,f) %>%
    mutate(f=fct_relevel(f, "At least once a day")) %>%
    filter(f!="Don’t Know") %>%
    group_by(wave) %>%
    mutate(total=sum(n), 
           prop=n/total)

ggplot(usage, 
       aes(y=prop,
           x=f)) +
    geom_bar(width = .8, color="black",
             position = position_dodge(width = 0.6),
             alpha=.8, 
             color="black",
             stat="identity", 
             fill=treat) +
    xlab("\n Frequency of Media Consumption about Politics and Elections on WhatsApp")+
    ylab("Share of Valid Responses") +
    labs(title="") +
    theme(axis.title.x = element_text(hjust=0.5, face="italic"),
          axis.text = element_text(size=12, angle = 45, hjust=1),
          strip.text = element_text(family = my_font, 
                                    color = "#22211d",
                                    size = 14, 
                                    face="italic"), 
          strip.background = element_rect(fill="gray92", color="gray92"))  

save_function("sm_fig17.png")
